let app = getApp()

Page({
  data: {
    avatarUrl: 'https://ucenter.ldtd168.com/?ctl=Index&met=img&user_id=12127',
    userName: '',
    genderIndex: 0,
    genders: ['男', '女'],
	nationIndex: 0,
    nations: [
  "汉族", "蒙古族", "回族", "藏族", "维吾尔族", "苗族", "彝族", "壮族", "布依族",
  "朝鲜族", "满族", "侗族", "瑶族", "白族", "土家族", "哈尼族", "哈萨克族", "傣族",
  "黎族", "傈僳族", "佤族", "畲族", "高山族", "拉祜族", "水族", "东乡族", "纳西族",
  "景颇族", "柯尔克孜族", "土族", "达斡尔族", "仫佬族", "羌族", "布朗族", "撒拉族",
  "毛南族", "仡佬族", "锡伯族", "阿昌族", "普米族", "塔吉克族", "怒族", "乌孜别克族",
  "俄罗斯族", "鄂温克族", "德昂族", "保安族", "裕固族", "京族", "塔塔尔族", "独龙族",
  "鄂伦春族", "赫哲族", "门巴族", "珞巴族", "基诺族"
],
    birthDate: '2025-04-16',
	workDate: '2025-04-16',
	phone: '',
	region: '',
    address: '',
  },

  onLoad() {
	new app.ToastPannel();
	
	  var that = this
    // 加载保存的用户信息
	that.requests();
  },
  requests:function(){
    var that = this
    var key = wx.getStorageSync('key')
    var uid = wx.getStorageSync('u')
    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Personal_Resume&met=resumeDetail&typ=json',
      method: 'GET',
      data: { k: key, u: uid },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data.status == 200) {
			const user_info = res.data.data.user_info
			that.setData({
				avatarUrl: user_info.avatarUrl,
				userName: user_info.userName,
				genderIndex: user_info.gender,
				nationIndex: user_info.nation,
				birthDate: user_info.birthDate,
				workDate: user_info.workDate,
				phone: user_info.phone,
				region: user_info.region,
				address: user_info.address
			})
        } else {
          that.show('数据加载失败')
        }
  
      }
    })
  },

	  // 选择头像
	  chooseAvatar() {
		var that = this
		wx.chooseImage({
		  count: 1,
		  sizeType: ['compressed'],
		  success: res => {
			wx.uploadFile({
			    url: app.configData.shop.api_url + '/index.php?ctl=Upload&action=uploadImage&typ=json', //仅为示例，非真实的接口地址
			    filePath: res.tempFilePaths[0],
			    name: 'upfile',
			    header: {
			        'content-type': 'multipart/form-data'
			    },
			    formData: {
			        'user': 'test'
			    },
				success: function (res) {
					var data = res.data
					var re = JSON.parse(res.data)
					that.setData({
					  avatarUrl: re.url
					})
				}
			})
		  }
		})
	  },
 // 输入事件处理
  handleName(e) {
    this.setData({
      userName: e.detail.value // 实时更新到data
    })
  },
  // 输入事件处理
   handlePhone(e) {
     this.setData({
       phone: e.detail.value // 实时更新到data
     })
   },
   // 输入事件处理
    handleAddress(e) {
      this.setData({
        address: e.detail.value // 实时更新到data
      })
    },
	  // 性别选择变化
	  genderChange(e) {
		this.setData({ genderIndex: e.detail.value[0] })
	  },
	  nationChange(e) {
	  		this.setData({ nationIndex: e.detail.value[0] })
	  },

	//生日日期
	bindDateChange(e) {
		this.setData({ birthDate: e.detail.value })
	},
	//参与工作日期
	bindWorkDateChange(e) {
		this.setData({ workDate: e.detail.value })
	},
	//省市区
	  bindRegionChange: function (e) {
		this.setData({
		  region: e.detail.value
		})
	  },

  // 保存用户信息
  saveUserInfo() {
	const that = this
	const key = wx.getStorageSync('key')
	const uid = wx.getStorageSync('u')
	if(that.data.userName == ''){
		that.show('请输入姓名')
	} else if(that.data.phone == '') {
		that.show('请输入联系电话')
	} else if(that.data.region == '') {
		that.show('请选择现居住地址')
	} else if(that.data.address == '') {
		that.show('请输入详细地址')
	} else {
		const userInfo = {
		  avatarUrl: that.data.avatarUrl,
		  userName: that.data.userName,
		  gender: that.data.genderIndex,
		  nation: that.data.nationIndex,
		  birthDate: that.data.birthDate,
		  workDate: that.data.workDate,
		  phone: that.data.phone,
		  region: that.data.region,
		  address: that.data.address
		}
		const parme = {
					k: key, 
					u: uid,
					user_info: JSON.stringify(userInfo),
				};
		wx.request({
		  url: app.configData.shop.api_url + '/index.php?ctl=Personal_Resume&met=addResumeUserInfo&typ=json',
		  data: parme,
		  method: 'POST', 
		  header: {
		    "Content-Type": "application/x-www-form-urlencoded"
		  }, // 设置请求的 header
		  success: function (res) {
		    if(res.data.status == 200){
		      that.show('保存成功')
		      wx.redirectTo({
		        url: '../personal_resume/personal_resume',
		      })
		    }else{
		      that.show('保存失败')
		    }
		  },
		  fail: function (res) {
		  
		  }
		})
	}
	
    
    // wx.setStorageSync('userInfo', userInfo)
  },

})